
source('utils.R')
source('read_dataset.R')

## inflation adjustment
cols = c('r_val','d_val','r_val_pcc_only','d_val_pcc_only')
df12[, (cols) := lapply(.SD, \(x) x * 1.12), .SDcols = cols]
df16[, (cols) := lapply(.SD, \(x) x * 1.037037), .SDcols = cols]

donors = list(
  d12_inds = open_dataset(glue('final_long/cycle=2012')) %>% filter(d_pcc > 0 | d_sp > 0 | d_jfc > 0) %>% pull(component),
  r12_inds = open_dataset(glue('final_long/cycle=2012')) %>% filter(r_pcc > 0 | r_sp > 0 | r_jfc > 0) %>% pull(component),
  d16_inds = open_dataset(glue('final_long/cycle=2016')) %>% filter(d_pcc > 0 | d_sp > 0 | d_jfc > 0) %>% pull(component),
  r16_inds = open_dataset(glue('final_long/cycle=2016')) %>% filter(r_pcc > 0 | r_sp > 0 | r_jfc > 0) %>% pull(component),
  d20_inds = open_dataset(glue('final_long/cycle=2020')) %>% filter(d_pcc > 0 | d_sp > 0 | d_jfc > 0) %>% pull(component),
  r20_inds = open_dataset(glue('final_long/cycle=2020')) %>% filter(r_pcc > 0 | r_sp > 0 | r_jfc > 0) %>% pull(component)
)

df12[,nat_quant_fine:=as.numeric(as.character(nat_quant_fine))]
df16[,nat_quant_fine:=as.numeric(as.character(nat_quant_fine))]
df20[,nat_quant_fine:=as.numeric(as.character(nat_quant_fine))]

df12[,nat_quant:=fcase(nat_quant_fine < .5, '[0,50)',
                       nat_quant_fine < 0.9, '[50,90)',
                       nat_quant_fine < .99, '[90,99)',
                       nat_quant_fine < .999, '[99,99.9)',
                       nat_quant_fine < 1.01, '[99.9,)',
                       default = NA_character_)]

df16[,nat_quant:=fcase(nat_quant_fine < .5, '[0,50)',
                       nat_quant_fine < 0.9, '[50,90)',
                       nat_quant_fine < .99, '[90,99)',
                       nat_quant_fine < .999, '[99,99.9)',
                       nat_quant_fine < 1.01, '[99.9,)',
                       default = NA_character_)]

df20[,nat_quant:=fcase(nat_quant_fine < .5, '[0,50)',
                       nat_quant_fine < 0.9, '[50,90)',
                       nat_quant_fine < .99, '[90,99)',
                       nat_quant_fine < .999, '[99,99.9)',
                       nat_quant_fine < 1.01, '[99.9,)',
                       default = NA_character_)]

# c1, c2 ------------------------------------------------------------------ (gender)

d12 = df12[component %chin% donors[['d12_inds']] &
             gender %chin% c('F', 'M'), .(d12 = sum(d_val, na.rm = T), d12n = .N), .(nat_quant, gender)]
r12 = df12[component %chin% donors[['r12_inds']] &
             gender %chin% c('F', 'M'), .(r12 = sum(r_val, na.rm = T), r12n = .N), .(nat_quant, gender)]

d16 = df16[component %chin% donors[['d16_inds']] &
             gender %chin% c('F', 'M'), .(d16 = sum(d_val, na.rm = T), d16n = .N), .(nat_quant, gender)]
r16 = df16[component %chin% donors[['r16_inds']] &
             gender %chin% c('F', 'M'), .(r16 = sum(r_val, na.rm = T), r16n = .N), .(nat_quant, gender)]

d20 = df20[component %chin% donors[['d20_inds']] &
             gender %chin% c('F', 'M'), .(d20 = sum(d_val, na.rm = T), d20n = .N), .(nat_quant, gender)]
r20 = df20[component %chin% donors[['r20_inds']] &
             gender %chin% c('F', 'M'), .(r20 = sum(r_val, na.rm = T), r20n = .N), .(nat_quant, gender)]

out = merge(d12,d16) %>% merge(r12) %>% merge(r16) %>% merge(d20) %>% merge(r20)

out[, Clinton := (d16n - d12n) / d12n]
out[, Trump16 := (r16n - r12n) / r12n]
out[, Trump20 := (r20n - r12n) / r12n]
out[, Biden := (d20n - d12n) / d12n]

c1 = melt(out[, c(1:2, 15:18)], 1:2)
c1 = c1[, .(f = value[gender == 'F'], m = value[gender =='M']), .(nat_quant, variable)]

out[, Clinton := (d16 - d12) / d12]
out[, Trump := (r16 - r12) / r12]
out[, Trump20 := (r20 - r12) / r12]
out[, Biden := (d20 - d12) / d12]
c2 = melt(out[, c(1:2, 15:18)], 1:2)
c2 = c2[, .(f = value[gender == 'F'], m = value[gender == 'M']), .(nat_quant, variable)]

# c3, c4 ------------------------------------------------------------------ (black)


d12 = df12[component %chin% donors[['d12_inds']] &
             ethnicity %chin% c('B', 'W'), .(d12 = sum(d_val, na.rm = T), d12n = .N), .(nat_quant, ethnicity)]
r12 = df12[component %chin% donors[['r12_inds']] &
             ethnicity %chin% c('B', 'W'), .(r12 = sum(r_val, na.rm = T), r12n = .N), .(nat_quant, ethnicity)]

d16 = df16[component %chin% donors[['d16_inds']] &
             ethnicity %chin% c('B', 'W'), .(d16 = sum(d_val, na.rm = T), d16n = .N), .(nat_quant, ethnicity)]
r16 = df16[component %chin% donors[['r16_inds']] &
             ethnicity %chin% c('B', 'W'), .(r16 = sum(r_val, na.rm = T), r16n = .N), .(nat_quant, ethnicity)]

d20 = df20[component %chin% donors[['d20_inds']] &
             ethnicity %chin% c('B', 'W'), .(d20 = sum(d_val, na.rm = T), d20n = .N), .(nat_quant, ethnicity)]
r20 = df20[component %chin% donors[['r20_inds']] &
             ethnicity %chin% c('B', 'W'), .(r20 = sum(r_val, na.rm = T), r20n = .N), .(nat_quant, ethnicity)]

out = merge(d12, d16) %>% merge(r12) %>% merge(r16) %>% merge(d20) %>% merge(r20)

out[, Clinton := (d16n - d12n) / d12n]
out[, Trump16 := (r16n - r12n) / r12n]
out[, Trump20 := (r20n - r12n) / r12n]
out[, Biden := (d20n - d12n) / d12n]

c3 = melt(out[, c(1:2, 15:18)], 1:2)
c3 = c3[, .(f = value[ethnicity == 'B'], m = value[ethnicity == 'W']), .(nat_quant, variable)]

out[, Clinton := (d16 - d12) / d12]
out[, Trump := (r16 - r12) / r12]
out[, Trump20 := (r20 - r12) / r12]
out[, Biden := (d20 - d12) / d12]

c4 = melt(out[, c(1:2, 15:18)], 1:2)
c4 = c4[, .(f = value[ethnicity == 'B'], m = value[ethnicity == 'W']), .(nat_quant, variable)]

# c5, c6 ------------------------------------------------------------------ (asian)

d12 = df12[component %chin% donors[['d12_inds']] &
             ethnicity %chin% c('A', 'W'), .(d12 = sum(d_val, na.rm = T), d12n = .N), .(nat_quant, ethnicity)]
r12 = df12[component %chin% donors[['r12_inds']] &
             ethnicity %chin% c('A', 'W'), .(r12 = sum(r_val, na.rm = T), r12n = .N), .(nat_quant, ethnicity)]

d16 = df16[component %chin% donors[['d16_inds']] &
             ethnicity %chin% c('A', 'W'), .(d16 = sum(d_val, na.rm = T), d16n = .N), .(nat_quant, ethnicity)]
r16 = df16[component %chin% donors[['r16_inds']] &
             ethnicity %chin% c('A', 'W'), .(r16 = sum(r_val, na.rm = T), r16n = .N), .(nat_quant, ethnicity)]

d20 = df20[component %chin% donors[['d20_inds']] &
             ethnicity %chin% c('A', 'W'), .(d20 = sum(d_val, na.rm = T), d20n = .N), .(nat_quant, ethnicity)]
r20 = df20[component %chin% donors[['r20_inds']] &
             ethnicity %chin% c('A', 'W'), .(r20 = sum(r_val, na.rm = T), r20n = .N), .(nat_quant, ethnicity)]

out = merge(d12, d16) %>% merge(r12) %>% merge(r16) %>% merge(d20) %>% merge(r20)

out[, Clinton := (d16n - d12n) / d12n]
out[, Trump16 := (r16n - r12n) / r12n]
out[, Trump20 := (r20n - r12n) / r12n]
out[, Biden := (d20n - d12n) / d12n]

c5 = melt(out[, c(1:2, 15:18)], 1:2)
c5 = c5[, .(f = value[ethnicity == 'A'], m = value[ethnicity == 'W']), .(nat_quant, variable)]

out[, Clinton := (d16 - d12) / d12]
out[, Trump := (r16 - r12) / r12]
out[, Trump20 := (r20 - r12) / r12]
out[, Biden := (d20 - d12) / d12]

c6 = melt(out[, c(1:2, 15:18)], 1:2)
c6 = c6[, .(f = value[ethnicity == 'A'], m = value[ethnicity == 'W']), .(nat_quant, variable)]

# c7, c8 ------------------------------------------------------------------ (latino)

d12 = df12[component %chin% donors[['d12_inds']] &
             ethnicity %chin% c('H', 'W'), .(d12 = sum(d_val, na.rm = T), d12n = .N), .(nat_quant, ethnicity)]
r12 = df12[component %chin% donors[['r12_inds']] &
             ethnicity %chin% c('H', 'W'), .(r12 = sum(r_val, na.rm = T), r12n = .N), .(nat_quant, ethnicity)]

d16 = df16[component %chin% donors[['d16_inds']] &
             ethnicity %chin% c('H', 'W'), .(d16 = sum(d_val, na.rm = T), d16n = .N), .(nat_quant, ethnicity)]
r16 = df16[component %chin% donors[['r16_inds']] &
             ethnicity %chin% c('H', 'W'), .(r16 = sum(r_val, na.rm = T), r16n = .N), .(nat_quant, ethnicity)]

d20 = df20[component %chin% donors[['d20_inds']] &
             ethnicity %chin% c('H', 'W'), .(d20 = sum(d_val, na.rm = T), d20n = .N), .(nat_quant, ethnicity)]
r20 = df20[component %chin% donors[['r20_inds']] &
             ethnicity %chin% c('H', 'W'), .(r20 = sum(r_val, na.rm = T), r20n = .N), .(nat_quant, ethnicity)]

out = merge(d12, d16) %>% merge(r12) %>% merge(r16) %>% merge(d20) %>% merge(r20)

out[, Clinton := (d16n - d12n) / d12n]
out[, Trump16 := (r16n - r12n) / r12n]
out[, Trump20 := (r20n - r12n) / r12n]
out[, Biden := (d20n - d12n) / d12n]

c7 = melt(out[, c(1:2, 15:18)], 1:2)
c7 = c7[, .(f = value[ethnicity == 'H'], m = value[ethnicity == 'W']), .(nat_quant, variable)]

out[, Clinton := (d16 - d12) / d12]
out[, Trump := (r16 - r12) / r12]
out[, Trump20 := (r20 - r12) / r12]
out[, Biden := (d20 - d12) / d12]

c8 = melt(out[, c(1:2, 15:18)], 1:2)
c8 = c8[, .(f = value[ethnicity == 'H'], m = value[ethnicity == 'W']), .(nat_quant, variable)]

# c9, c10 ----------------------------------------------------------------- (non-col white)

ed = c('HS D', 'Voc', 'Less', 'Some', 'Bach', 'Grad')

d12 = df12[component %chin% donors[['d12_inds']] &
             ethnicity == 'W' &
             education %chin% ed, .(d12 = sum(d_val, na.rm = T), d12n = .N), .(nat_quant, education %chin% c('Bach', 'Grad'))]
r12 = df12[component %chin% donors[['r12_inds']] &
             ethnicity == 'W' &
             education %chin% ed, .(r12 = sum(r_val, na.rm = T), r12n = .N), .(nat_quant, education %chin% c('Bach', 'Grad'))]

d16 = df16[component %chin% donors[['d16_inds']] &
             ethnicity == 'W' &
             education %chin% ed, .(d16 = sum(d_val, na.rm = T), d16n = .N), .(nat_quant, education %chin% c('Bach', 'Grad'))]
r16 = df16[component %chin% donors[['r16_inds']] &
             ethnicity == 'W' &
             education %chin% ed, .(r16 = sum(r_val, na.rm = T), r16n = .N), .(nat_quant, education %chin% c('Bach', 'Grad'))]

d20 = df20[component %chin% donors[['d20_inds']] &
             ethnicity == 'W' &
             education %chin% ed, .(d20 = sum(d_val, na.rm = T), d20n = .N), .(nat_quant, education %chin% c('Bach', 'Grad'))]
r20 = df20[component %chin% donors[['r20_inds']] &
             ethnicity == 'W' &
             education %chin% ed, .(r20 = sum(r_val, na.rm = T), r20n = .N), .(nat_quant, education %chin% c('Bach', 'Grad'))]

out = merge(d12, d16) %>% merge(r12) %>% merge(r16) %>% merge(d20) %>% merge(r20)

out[, Clinton := (d16n - d12n) / d12n]
out[, Trump16 := (r16n - r12n) / r12n]
out[, Trump20 := (r20n - r12n) / r12n]
out[, Biden := (d20n - d12n) / d12n]

c9 = melt(out[, c(1:2, 15:18)], 1:2)
c9 = c9[, .(f = value[education == F], m = value[education == T]), .(nat_quant, variable)]

out[, Clinton := (d16 - d12) / d12]
out[, Trump := (r16 - r12) / r12]
out[, Trump20 := (r20 - r12) / r12]
out[, Biden := (d20 - d12) / d12]
c10 = melt(out[, c(1:2, 15:18)], 1:2)
c10 = c10[, .(f = value[education == F], m = value[education == T]), .(nat_quant, variable)]

# c11, c12 ---------------------------------------------------------------- (white/poc)

eth = c('W', 'B', 'A', 'H', 'O')

d12 = df12[component %chin% donors[['d12_inds']] &
             ethnicity %chin% eth, .(d12 = sum(d_val, na.rm = T), d12n = .N), .(nat_quant, ethnicity == 'W')]
r12 = df12[component %chin% donors[['r12_inds']] &
             ethnicity %chin% eth, .(r12 = sum(r_val, na.rm = T), r12n = .N), .(nat_quant, ethnicity == 'W')]

d16 = df16[component %chin% donors[['d16_inds']] &
             ethnicity %chin% eth, .(d16 = sum(d_val, na.rm = T), d16n = .N), .(nat_quant, ethnicity == 'W')]
r16 = df16[component %chin% donors[['r16_inds']] &
             ethnicity %chin% eth, .(r16 = sum(r_val, na.rm = T), r16n = .N), .(nat_quant, ethnicity == 'W')]

d20 = df20[component %chin% donors[['d20_inds']] &
             ethnicity %chin% eth, .(d20 = sum(d_val, na.rm = T), d20n = .N), .(nat_quant, ethnicity == 'W')]
r20 = df20[component %chin% donors[['r20_inds']] &
             ethnicity %chin% eth, .(r20 = sum(r_val, na.rm = T), r20n = .N), .(nat_quant, ethnicity == 'W')]

out = merge(d12, d16) %>% merge(r12) %>% merge(r16) %>% merge(d20) %>% merge(r20)

out[, Clinton := (d16n - d12n) / d12n]
out[, Trump16 := (r16n - r12n) / r12n]
out[, Trump20 := (r20n - r12n) / r12n]
out[, Biden := (d20n - d12n) / d12n]

c11 = melt(out[, c(1:2, 15:18)], 1:2)
c11 = c11[, .(f = value[ethnicity == F], m = value[ethnicity == T]), .(nat_quant, variable)]

out[, Clinton := (d16 - d12) / d12]
out[, Trump := (r16 - r12) / r12]
out[, Trump20 := (r20 - r12) / r12]
out[, Biden := (d20 - d12) / d12]

c12 = melt(out[, c(1:2, 15:18)], 1:2)
c12 = c12[, .(f = value[ethnicity == F], m = value[ethnicity == T]), .(nat_quant, variable)]

save(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, file = 'summary_data/figs7_8_A12_A13_A14.rda')

